from pynput import keyboard
from threading import Thread
import win32con, win32gui
from winguiauto import findTopWindow
import pyautogui
import time


def listen():  # 键盘监听函数
    print("listening")
    def on_press(key):
        if key == keyboard.Key.f1:
            print("f1")
            combo1()
        elif key == keyboard.Key.f2:
            combo2()
        elif key == keyboard.Key.f3:
            combo3()
        elif key == keyboard.Key.f4:
            combo4()
        elif key == keyboard.Key.f5:
            combo5()
        elif key == keyboard.Key.f6:
            combo6()
        elif key == keyboard.Key.f7:
            pass
        elif key == keyboard.Key.f8:
            pass
        elif key == keyboard.Key.f9:
            exit(0)
    def on_release(key):

        pass
        # print("release:", key)

    with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
        listener.join()

def combo1():
    hwnd = findTopWindow("炉石传说")
    win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0,0,0,0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
    rect = win32gui.GetWindowPlacement(hwnd)[-1]
    # print(rect)
    currentMouseX, currentMouseY = pyautogui.position()
    w = rect[2] - rect[0]
    h = rect[3] - rect[1]
    print("{}, {}".format(abs(rect[0] - currentMouseX), abs(rect[1] - currentMouseY)))
    print("{}, {}".format(abs(rect[0] - currentMouseX) / w, abs(rect[1] - currentMouseY) / h))

def combo2():
    print("进入副本")
    l = [
        # (0.41244239631336405, 0.38317757009345793), # 空气元素坐标
        # (0.2368827160493827, 0.7032181168057211), # 哈拉樊
        (0.23655913978494625, 0.3621495327102804), # 1-1
        (0.8202764976958525, 0.7838785046728972), # 点击选择
        (0.3579109062980031, 0.3469626168224299), # 点击第二队
        (0.7549923195084486, 0.8376168224299065), # 点击确认
        (0.4377880184331797, 0.5864485981308412), # 点击锁定
    ]
    cnt = 0
    for pos in l:
        pyautogui.click(transpos(pos))
        if cnt == 1:
            time.sleep(1)
        time.sleep(0.5)
        cnt += 1

def combo3():
    print("释放三个技能")
    l = [
        (0.4001536098310292, 0.4649532710280374), # 第一个技能
        (0.49923195084485406, 0.47079439252336447), # 第二个技能
        (0.6036866359447005, 0.47079439252336447), # 第三个技能
    ]
    l2 = [
        (0.40860215053763443, 0.6799065420560748), # 第一个人物
        (0.500768049155146, 0.6857476635514018), # 第二个人物
        (0.5929339477726574, 0.6752336448598131), # 第三个人物
    ]
    skill = [0, 1, 0]
    cnt = 0
    for i in skill:
        # pyautogui.click(transpos(l2[cnt]))
        # time.sleep(1)
        pyautogui.click(transpos(l[i]))
        time.sleep(1)
        cnt += 1
    time.sleep(2)
    combo4()

def single_hero(skill):
    print("释放一个技能")
    l = [
        (0.4001536098310292, 0.4649532710280374), # 第一个技能
        (0.49923195084485406, 0.47079439252336447), # 第二个技能
        (0.6036866359447005, 0.47079439252336447), # 第三个技能
    ]
    l2 = [
        (0.40860215053763443, 0.6799065420560748), # 第一个人物
        (0.500768049155146, 0.6857476635514018), # 第二个人物
        (0.5929339477726574, 0.6752336448598131), # 第三个人物
    ]
    pyautogui.click(transpos(l[skill]))
    time.sleep(1)
    pyautogui.click(transpos((0.4009216589861751, 0.5350467289719626))) # 德鲁伊三技能
    time.sleep(1)
    combo4()



def combo4():
    print("按回合结束按钮")
    pyautogui.click(transpos((0.8463901689708141, 0.47897196261682246)))
    # time.sleep(1)
    pyautogui.click(transpos((0.8463901689708141, 0.47897196261682246)))
    print("按开始打怪按钮")
    pyautogui.click(transpos((0.8225806451612904, 0.7885514018691588)))

def combo5():
    # print("选择中间的宝藏")
    print("选择宝藏")
    pyautogui.click(transpos((0.6013824884792627, 0.5116822429906542)))
    print("确认宝藏")
    pyautogui.click(transpos((0.6029185867895546, 0.794392523364486)))

def combo6():
    print("放弃副本")
    l = [
        (0.40168970814132104, 0.9217289719626168), # 查看队伍
        (0.5798771121351767, 0.7348130841121495), # 点击放弃
        (0.4293394777265745, 0.5829439252336449), # 确认放弃
        (0.510752688172043, 0.6939252336448598), # 确认安慰奖励
    ]
    for pos in l:
        pyautogui.click(transpos(pos))
        time.sleep(1)
    # time.sleep(1)
    pyautogui.click(transpos(l[3]))

def choose_road(side):
    locs = {
        'left': [(280, 430), (323, 430), (383, 430)],
        'right': [(614, 430), (661, 430), (720, 430)]
    }
    # locs = {
    #     'left': [(323, 430)],
    #     'right': [(661, 430)]
    # }
    for loc in locs[side]:
        pyautogui.moveTo(transpos(loc))
        pyautogui.click(clicks=2, interval=0.25)
        combo4()

def click_visitor(num):
    l = [
        (0.315668202764977, 0.4427570093457944), # 第一个访客
        (0.4946236559139785, 0.44742990654205606), # 第二个访客
        (0.6620583717357911, 0.42990654205607476), # 第三个访客
        (0.4869431643625192, 0.6962616822429907), # 确定访客
    ]
    pyautogui.click(transpos(l[num]))
    time.sleep(0.5)
    pyautogui.click(transpos(l[3]))
    time.sleep(2)
    pyautogui.click(transpos(l[3]))

def transpos(pos):
    hwnd = findTopWindow("炉石传说")
    win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0,0,0,0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
    rect = win32gui.GetWindowPlacement(hwnd)[-1]
    w = rect[2] - rect[0]
    h = rect[3] - rect[1]
    if pos[0] < 1.1:
        return (rect[0] + w * pos[0], rect[1] + h * pos[1])
    else:
        return (rect[0] + pos[0], rect[1] + pos[1])

def gift():
    l = [
        (0.5245775729646698, 0.3399532710280374), # 正上方宝箱
        (0.3602150537634409, 0.7067757009345794), # 左边宝箱
        (0.7081413210445469, 0.7675233644859814), # 右边宝箱
        (0.521505376344086, 0.5794392523364486), # 确定按钮
        (0.4976958525345622, 0.8084112149532711), # 确认悬赏完成
    ]
    for pos in l:
        pyautogui.click(transpos(pos))
        time.sleep(1)
    time.sleep(2)
    pyautogui.click(transpos(l[-1]))

class ListenThread(Thread):

    def __init__(self):
        super().__init__()

    def run(self):
        listen()


if __name__ == "__main__":
    listenThread = ListenThread()  # 创建监听线程
    listenThread.start()

